#pragma once
#include "EXHeaderCtrlResSet.h"

// CEXHeaderCtrl

class AFX_EXT_CLASS CEXHeaderCtrl : public CHeaderCtrl
{
	DECLARE_DYNAMIC(CEXHeaderCtrl)

	class CXMemDC : public CDC
	{
	public:
		CXMemDC(CDC* pDC) : CDC()
		{
			ASSERT(pDC != NULL);

			m_pDC = pDC;
			m_pOldBitmap = NULL;
			m_bMemDC = !pDC->IsPrinting();

			if (m_bMemDC)
			{
				pDC->GetClipBox(&m_rect);
				CreateCompatibleDC(pDC);
				m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
				m_pOldBitmap = SelectObject(&m_bitmap);
				SetWindowOrg(m_rect.left, m_rect.top);
			}
			else
			{
				m_bPrinting = pDC->m_bPrinting;
				m_hDC       = pDC->m_hDC;
				m_hAttribDC = pDC->m_hAttribDC;
			}
		}

		~CXMemDC()
		{
			if (m_bMemDC) 
			{    
			    m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
					this, m_rect.left, m_rect.top, SRCCOPY);

				  SelectObject(m_pOldBitmap);
			} 
      else 
      {
				m_hDC = m_hAttribDC = NULL;
			}
		}

		CXMemDC* operator->() {return this;}

		operator CXMemDC*() {return this;}

	private:
		CBitmap  m_bitmap;
		CBitmap* m_pOldBitmap;  
		CDC*     m_pDC;         
		CRect    m_rect;        
		BOOL     m_bMemDC;      
	};

public:
  enum HEADER_STYLE
  {
    NOIMAGE,
    HEADER_CTRL = 15,
  };
	CEXHeaderCtrl();
	virtual ~CEXHeaderCtrl();

	// Attributes
public:
	void SetListCtrl(CListCtrl *pListCtrl) { m_pListCtrl = pListCtrl; }
	BOOL ModifyProperty(WPARAM wParam, LPARAM lParam);
	int GetSpacing() { return m_iSpacing; }
	void SetSpacing(int nSpacing) { m_iSpacing = nSpacing; }
	UINT GetAlignment() { return m_nFormat; }
	void SetAlignment(UINT nFormat) { m_nFormat = nFormat; }
	COLORREF GetTextColor() { return m_rgbText; }
	void SetTextColor(COLORREF rgbText) { m_rgbText = rgbText; }
	BOOL GetDividerLines() { return m_bDividerLines; }
	void EnableDividerLines(BOOL bEnable) { m_bDividerLines = bEnable; }
	void SetHeaderBitmap(UINT nID);

	void SetBkgndColor(COLORREF crHeader);

	void DrawMouseDown(CDC* pDC, CRect rect);
	void DrawMouseOut(CDC* pDC, CRect rect);
	void DrawMouseOver(CDC* pDC, CRect rect);

	CBitmap m_bmpFocus;
	CBitmap m_bmpSelect;
	CBitmap m_bmpNormal;

	// Overrides
public:
	virtual void DrawItem(LPDRAWITEMSTRUCT);
	virtual void DrawItem(CDC* pDC, CRect rect, LPHDITEM lphdi);

  //ysy
  void SetResSet(CEXBaseResSet* pResSet);
  CEXHeaderCtrlResSet* m_pHeaderResSet;

protected:
	CListCtrl * m_pListCtrl;
	UINT m_nFormat;
	COLORREF m_rgbText;
	BOOL m_bDividerLines;
	BOOL m_bStaticBorder;
	BOOL m_bResizing;
	int m_iSpacing;
	SIZE m_sizeImage;
	SIZE m_sizeArrow;
	UINT m_nDontDropCursor;
	UINT m_nClickFlags;
	CPoint m_ptClickPoint;

	COLORREF m_cr3DHighLight;
	COLORREF m_cr3DShadow;
	COLORREF m_cr3DFace;
	COLORREF m_crBtnText;

	BOOL m_bLMouseDown;
	BOOL m_bTracking;
	UINT m_uHeaderBitmapID;

	void DrawCtrl(CDC* pDC);
	int DrawImage(CDC* pDC, CRect rect, LPHDITEM hdi, BOOL bRight);
	int DrawBitmap(CDC* pDC, CRect rect, LPHDITEM hdi, CBitmap* pBitmap, 
                BITMAP* pBitmapInfo, BOOL bRight);
	int DrawText (CDC* pDC, CRect rect, LPHDITEM lphdi);

	// Implementation
protected:
	DECLARE_MESSAGE_MAP()
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnPaint();
	afx_msg void OnSysColorChange();
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg LRESULT OnDeleteItem(WPARAM wparam, LPARAM lparam);
	afx_msg LRESULT OnInsertItem(WPARAM wparam, LPARAM lparam);
	afx_msg LRESULT OnLayout(WPARAM wparam, LPARAM lparam);
	afx_msg LRESULT OnSetImageList(WPARAM wparam, LPARAM lparam);
public:
	afx_msg void OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnDefCustomizeHeaderNotify();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//	afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point);
//	afx_msg void OnNcLButtonUp(UINT nHitTest, CPoint point);
	afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);
	afx_msg void OnWindowPosChanged(WINDOWPOS* lpwndpos);
};


